﻿using System;
using System.ComponentModel;

namespace SteriaMummert.SharePoint.SoftwareFactoryLite.Converters
{
    class ParentContentType
    {
        public string name = "";
        public string ID = "";

        public ParentContentType(string aname, string aID)
        {
            name = aname;
            ID = aID;
        }
    }
    /// <summary>
    /// returns available scopes for a feature
    /// </summary>
    public class InheritedContentTypeConverter : TypeConverter
    {
        static ParentContentType[] validValues;
       

        public InheritedContentTypeConverter()
        {
            validValues = new ParentContentType[] {
            new ParentContentType("Document","123"), new ParentContentType("Page","456")  };

        }

        
        public override bool IsValid(ITypeDescriptorContext context, object value)
        {
            return true;
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true; //no other values allowed
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            string[] standardvalues = new string[validValues.Length];
            for (int i = 0; i < validValues.Length; i++)
            {
                standardvalues[i] = validValues[i].name;
            }
            return new StandardValuesCollection(standardvalues);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(System.String))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            try
            {
                for (int i = 0; i < validValues.Length; i++)
                {
                    if (validValues[i].name == value.ToString())
                    {
                        return validValues[i].ID;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return value;
        }
    }
}
